home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / CRYPT.C < prev    next >
Text File  |  1992-12-02  |  11KB  |  502 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  */
  55.  
  56. #include    "SystemPub.h"
  57. #include    "Proc.h"
  58. #include    "ShellPub.h"
  59. #include    "Path.h"
  60.  
  61. #define        CryptAbort        (**MyShell).Proc[ProcID].bflags.f0
  62.  
  63.  
  64. #define        ROTORSIZE    256
  65. #define        MASK        0xFF
  66. #define        EMPTY        (unsigned char)0xFF
  67. #define        BUFSIZE        256L
  68.  
  69. #define        X_SIZE        256
  70.  
  71. unsigned     char        x[ X_SIZE ];
  72. unsigned    char        rotor1[ ROTORSIZE ];
  73. unsigned    char        rotor2[ ROTORSIZE ];
  74. unsigned    char        rotor3[ ROTORSIZE ];
  75.  
  76. /********************************************************************/
  77.  
  78. void    InvertRotor( unsigned char *r )
  79. {
  80. unsigned char t[ ROTORSIZE ];
  81. int16    i;
  82.  
  83.     for ( i = 0; i < ROTORSIZE; i++ )
  84.         t[ i ] = r[ i ];
  85.         
  86.     for ( i = 0; i < ROTORSIZE; i++ )
  87.         r[ t[ i ] ] = i;
  88. }
  89.  
  90. /********************************************************************/
  91.  
  92. void    BeatIt( char *target, char *eggs, int16 end )
  93. {
  94. char    *cp1, *cp2;
  95. int16    len = 0;
  96.  
  97.     cp1 = target;
  98.     while( *cp1 && len < end )
  99.         {
  100.         cp2 = eggs;
  101.         while( *cp2 )
  102.             {
  103.             *cp1 += *cp2;
  104.             cp2++;
  105.             }
  106.         
  107.         len++;
  108.         cp1++;
  109.         }
  110.     
  111.     while( len++ < end )
  112.         *cp1++ = *cp2 + 1;
  113. }
  114.  
  115. /********************************************************************/
  116.  
  117. void        InitCrypt( char *password, int16 decrypt )
  118. {
  119. int16        index;
  120. unsigned     i;
  121. unsigned    random;
  122. int32        seed = 123L;
  123. char        buf[14];
  124. char        key[9];
  125. char        salt[3];
  126.  
  127.     password[ 13 ] = '\0';
  128.     for( i = 0; i < 14; i++ )
  129.         buf[ i ] = '\0';
  130.         
  131.     strcpy( buf, password );
  132.     salt[0] = buf[0];
  133.     salt[1] = buf[1];
  134.     salt[2] = '\0';
  135.     
  136.     BeatIt( buf, salt, 13 );
  137.  
  138.     for( i = 0 ; i < ROTORSIZE; i++ )
  139.         rotor1[i] = rotor2[i] = rotor3[i] = EMPTY;
  140.  
  141.     for( i = 2; i < 13; i++ )
  142.         seed = seed * buf[i] + i;
  143.  
  144.     i = 0;
  145.     while( i < ROTORSIZE )
  146.         {
  147.         seed = (int32)(5L * seed + (int32)i);
  148.         random = (unsigned)( seed % 65521L);
  149.         index = (int16)(random & MASK);
  150.         if (rotor1[index] == EMPTY)
  151.             rotor1[index] = ( unsigned char ) i++;
  152.         else
  153.             continue;
  154.         }
  155.  
  156.     i = 0;
  157.     while( i < ROTORSIZE )
  158.         {
  159.         seed = (int32)(5L * seed + (int32)i);
  160.         random = (unsigned)( seed % 65521L);
  161.         index = (int16)(random & MASK);
  162.         if (rotor2[index] == EMPTY)
  163.             rotor2[index] = ( unsigned char ) i++;
  164.         else
  165.             continue;
  166.         }
  167.  
  168.     i = 0;
  169.     while( i < ROTORSIZE )
  170.         {
  171.         seed = (int32)(5L * seed + (int32)i);
  172.         random = (unsigned)( seed % 65521L);
  173.         index = (int16)(random & MASK);
  174.         if (rotor3[index] == EMPTY)
  175.             rotor3[index] = ( unsigned char ) i++;
  176.         else
  177.             continue;
  178.         }
  179.         
  180.     for( i = 0; i < X_SIZE; i++ )
  181.         {
  182.         seed = (int32)(5L * seed + (int32)i);
  183.         random = (unsigned)(seed % 65521L);
  184.         x[i] = random & 3;
  185.         }
  186.  
  187.     if( decrypt )
  188.         {
  189.         InvertRotor( rotor1 );
  190.         InvertRotor( rotor2 );
  191.         InvertRotor( rotor3 );
  192.         }
  193. }
  194.  
  195. /********************************************************************/
  196.  
  197. void        EncryptFile( WHandle ShellWh, int16 ProcID, int16 srcRefNum,
  198.                 int16 dstRefNum )
  199. {
  200. int16        i = 0;
  201. unsigned    char    *ch, *cp, inBuf[ BUFSIZE ], outBuf[ BUFSIZE ];
  202. unsigned    char    ofs1 = 0, ofs2 = 0, ofs3 = 0;
  203. OSErr        err = noErr;
  204. int32        count, index;
  205. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  206.  
  207.     while( (err == noErr) && !CryptAbort )
  208.         {
  209.         if( UserAbort() )
  210.             CryptAbort = TRUE;
  211.             
  212.         count = BUFSIZE;
  213.         err = FSRead( srcRefNum, &count, inBuf );
  214.         
  215.         ch         = inBuf;
  216.         cp         = outBuf;
  217.         index     = count;
  218.         
  219.         for( index = 0; index < count; index++ )
  220.             {
  221.             *cp++ = (char) 
  222.                 rotor3[ (unsigned)
  223.                     (rotor2[ (unsigned)
  224.                         (rotor1[ ((unsigned) *ch + ofs1) & MASK ]
  225.                     +ofs2) & MASK ])
  226.                 +ofs3 & MASK ];
  227.     
  228.             switch( x[i] )
  229.                 {
  230.                 case 0:    ofs1 = ++ofs1 & MASK;    break;
  231.                 case 1:    ofs2 = ++ofs2 & MASK;    break;
  232.                 case 2:    ofs3 = ++ofs3 & MASK;    break;
  233.                 }
  234.     
  235.             if( ofs1 == 0 )
  236.                 ofs2 = ++ofs2 & MASK;
  237.             if (ofs2 == 0)
  238.                 ofs3 = ++ofs3 & MASK;
  239.                 
  240.             if( ++i == X_SIZE )
  241.                 i = 0;
  242.             ch++;
  243.             }
  244.             
  245.         FSWrite( dstRefNum, &count, outBuf );
  246.         }
  247. }
  248.  
  249. /********************************************************************/
  250.  
  251. void        DecryptFile( WHandle ShellWh, int16 ProcID, int16 srcRefNum,
  252.                 int16 dstRefNum )
  253. {
  254. int16        i = 0;
  255. unsigned    char    *ch, *cp, inBuf[ BUFSIZE ], outBuf[ BUFSIZE ];
  256. unsigned    char    ofs1 = 0, ofs2 = 0, ofs3 = 0;
  257. OSErr        err = noErr;
  258. int32        count, index;
  259. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  260.  
  261.     while( (err == noErr) && !CryptAbort )
  262.         {
  263.         if( UserAbort() )
  264.             CryptAbort = TRUE;
  265.             
  266.         count = BUFSIZE;
  267.         err = FSRead( srcRefNum, &count, inBuf );
  268.         
  269.         ch         = inBuf;
  270.         cp         = outBuf;
  271.         index     = count;
  272.         
  273.         for( index = 0; index < count; index++ )
  274.             {
  275.             *cp++ = (char)
  276.                 (rotor1[ (unsigned)
  277.                     (rotor2[ (unsigned)
  278.                         (rotor3[ (unsigned) (*ch) ] - ofs3) & MASK
  279.                         ] - ofs2) & MASK
  280.                     ] - ofs1) & MASK;
  281.     
  282.             switch( x[ i ] )
  283.                 {            
  284.                 case 0:    ofs1 = ++ofs1 & MASK;    break;
  285.                 case 1:    ofs2 = ++ofs2 & MASK;    break;
  286.                 case 2:    ofs3 = ++ofs3 & MASK;    break;
  287.                 }
  288.     
  289.             if( ofs1 == 0 )
  290.                 ofs2 = ++ofs2 & MASK;
  291.             if (ofs2 == 0)
  292.                 ofs3 = ++ofs3 & MASK;
  293.                 
  294.             if( ++i == X_SIZE )
  295.                 i = 0;
  296.             ch++;
  297.             }
  298.         
  299.         FSWrite( dstRefNum, &count, outBuf );
  300.         }
  301. }
  302.  
  303. /*******************************************************************
  304.  *
  305.  *    Function Crypt
  306.  *
  307.  *    PathName Callback function
  308.  *
  309.  *    usage Crypt [options] [names]    
  310.  *
  311.  *
  312.  *******************************************************************/
  313.  
  314. static    Boolean        encrypt, fixSuppress, commandPW;
  315.  
  316. void    CryptCallBack( WHandle ShellWh, int16 ProcID, char *path,
  317.                  char *last, pathType what, int16 vRefNum, int32 dirID )
  318. {
  319. int16    srcRefNum, dstRefNum, tempDst = FALSE;
  320. char    buf[ 64 ];
  321. OSErr    err;
  322.  
  323.     if( what == pathIsFile )
  324.         {
  325.         if( encrypt )
  326.             {
  327.             srcRefNum = OpenFileDirect( last, 'TEXT', fsRdPerm );
  328.  
  329.             if( srcRefNum )
  330.                 {
  331.                 sprintf( buf, "%s.crypt", last );
  332.                 CreateFile( buf, 'TEXT' );
  333.                 dstRefNum = OpenFileDirect( buf, 'TEXT', fsWrPerm );
  334.  
  335.                 if( dstRefNum )
  336.                     {
  337.                     EncryptFile( ShellWh, ProcID, srcRefNum, dstRefNum );
  338.                     FSClose( dstRefNum );
  339.                     }
  340.                 
  341.                 FSClose( srcRefNum );
  342.                 }
  343.             }
  344.         else
  345.             {
  346.             sprintf( buf, "%s.crypt", last );
  347.             srcRefNum = OpenFileDirect( buf, 'TEXT', fsRdPerm );
  348.             
  349.             if( srcRefNum == 0 )
  350.                 srcRefNum = OpenFileDirect( last, 'TEXT', fsRdPerm );
  351.                 
  352.             if( srcRefNum )
  353.                 {
  354.                 sprintf( buf, "%s.decrypt", last );
  355.                 CreateFile( buf, 'TEXT' );
  356.                 dstRefNum = OpenFileDirect( buf, 'TEXT', fsWrPerm );
  357.  
  358.                 if( dstRefNum )
  359.                     {
  360.                     DecryptFile( ShellWh, ProcID, srcRefNum, dstRefNum );
  361.                     FSClose( dstRefNum );
  362.                     }
  363.  
  364.                 FSClose( srcRefNum );
  365.                 }
  366.             }
  367.             
  368.         if( fixSuppress )
  369.             {
  370.             char    buf2[ 64 ];
  371.             /* copy the file back to the original name */
  372.             /* rename the original file */
  373.             strcpy( buf, last );
  374.             CtoPstr( buf );
  375.             
  376.             err = HRename( vRefNum, dirID, buf, "\p____temp____" );
  377.             
  378.             if( !err )
  379.                 {
  380.                 /* rename the encrypted file */
  381.                 if( encrypt )
  382.                     sprintf( buf2, "%s.crypt", last );
  383.                 else
  384.                     sprintf( buf2, "%s.decrypt", last );
  385.                 CtoPstr( buf2 );
  386.                 err = HRename( vRefNum, dirID, buf2, buf );
  387.                 }
  388.                 
  389.             /* delete the renamed original file */
  390.             if( !err )
  391.                 err = HDelete( vRefNum, dirID, "\p____temp____" );
  392.             }
  393.         }
  394. }
  395.  
  396. /*******************************************************************/
  397.  
  398. void        DoCryptFile( WHandle ShellWh, int16 ProcID, char *argument )
  399. {
  400. ShellWindRec    **MyShell;
  401.  
  402.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  403.  
  404.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) CryptCallBack,
  405.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  406.  
  407.     ResetShellPWD( ShellWh );
  408. }
  409.  
  410. /*******************************************************************/
  411.  
  412. Boolean        DoCRYPT( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  413.                     char *string )
  414. {
  415. int16            i, argc;
  416. char            *cp, argument[ 256 ];
  417. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  418.  
  419.     switch( ProcToken )
  420.         {
  421.         case    PROC_INIT    :
  422.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  423.             break;
  424.             
  425.         case    PROC_TERM    :
  426.         case    PROC_BREAK    :
  427.             CryptAbort = TRUE;
  428.             /* Tell the shell that we're done */
  429.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  430.             /* Turn ourself off */
  431.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  432.             break;
  433.             
  434.         case    PROC_STDIN    :
  435.             if( (**MyShell).Proc[ ProcID ].flags )
  436.                 {
  437.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  438.  
  439.                 /* get arguments */
  440.                 argc = (**MyShell).Proc[ ProcID ].argc;
  441.                 
  442.                 GetArgv( ShellWh, ProcID, 0, argument );
  443.                 encrypt = strcmp( "crypt", argument ) == 0;
  444.                 fixSuppress = FALSE;
  445.                 commandPW = FALSE;
  446.                 CryptAbort = FALSE;
  447.                 
  448.                 for( i = 1; i < argc; i++ )
  449.                     {
  450.                     GetArgv( ShellWh, ProcID, i, argument );
  451.                     cp = argument;
  452.         
  453.                     if( *cp++ == '-' )
  454.                         while( *cp && cp )
  455.                             switch( *cp++ )
  456.                                 {
  457.                                 case    'p'    :    /* password */
  458.                                     {
  459.                                     char    pw[ 256 ];
  460.                                     GetArgv( ShellWh, ProcID, ++i, pw );
  461.                                     InitCrypt( pw, !encrypt );
  462.                                     commandPW = TRUE;
  463.                                     cp = NULL;
  464.                                     }
  465.                                     break;
  466.                                     
  467.                                 case    's'    :    /* suppress */
  468.                                     fixSuppress = TRUE;
  469.                                     break;
  470.                                 }
  471.                     }
  472.  
  473.                 if( !commandPW )
  474.                     {
  475.                     char    pw[ 256 ];
  476.                     if( DialogAsk( "Your Password", pw ) )
  477.                         {
  478.                         InitCrypt( pw, !encrypt );
  479.                         commandPW = TRUE;
  480.                         }
  481.                     }
  482.                     
  483.                 if( commandPW )
  484.                     for( i = 1; i < argc; i++ )
  485.                         {
  486.                         GetArgv( ShellWh, ProcID, i, argument );
  487.                         if( *argument != '-' )
  488.                             DoCryptFile( ShellWh, ProcID, argument );
  489.                         else if( argument[ 1 ] == 'p' )
  490.                             i++;
  491.                         }
  492.  
  493.                 /* Tell the shell that we're done */
  494.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  495.                 
  496.                 /* Turn ourself off */
  497.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  498.                 return( FALSE );
  499.                 }
  500.         }
  501. }
  502.